﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Text.RegularExpressions;


public static class CacheHelper
{
    /// <summary>
    /// Insert value into the cache using
    /// appropriate name/value pairs
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="o">Item to be cached</param>
    /// <param name="key">Name of item</param>
    public static void Add<T>(T o, string key) where T : class
    {
        // NOTE: Apply expiration parameters as you see fit.
        // In this example, I want an absolute
        // timeout so changes will always be reflected
        // at that time. Hence, the NoSlidingExpiration.
        HttpContext.Current.Cache.Insert(
            key,
            o,
            null,
            DateTime.Now.AddMinutes(60),
            System.Web.Caching.Cache.NoSlidingExpiration);
    }
    /// <summary>
    /// 创建缓存项的文件依赖
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    /// <param name="fileName">文件绝对路径</param>
    public static void Insert(string key, object obj, string fileName)
    {
        //创建缓存依赖项
        CacheDependency dep = new CacheDependency(fileName);
        //创建缓存
        HttpContext.Current.Cache.Insert(key, obj, dep);
    }
    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void Clear(string key)
    {
        HttpContext.Current.Cache.Remove(key);
    }
    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void LikeClear(string key)
    {
        IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
        List<string> list = new List<string>();

        while (enumerator.MoveNext())
        {
            list.Add(enumerator.Key.ToString());
        }

        Regex reg = new Regex(key);
        foreach (string cKey in list)
        {
            if (reg.IsMatch(cKey))
            {
                HttpContext.Current.Cache.Remove(cKey);
            }
        }

    }

    /// <summary>
    /// Check for item in cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    /// <returns></returns>
    public static bool Exists(string key)
    {
        return HttpContext.Current.Cache[key] != null;
    }

    /// <summary>
    /// Retrieve cached item
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="key">Name of cached item</param>
    /// <returns>Cached item as type</returns>
    public static T Get<T>(string key) where T : class
    {
        try
        {
            return (T)HttpContext.Current.Cache[key];
        }
        catch
        {
            return null;
        }
    }
}
